
#!/bin/bash

CURRENT_DIRECTORY=`pwd`
export LIB_ROOT_DIR=${CURRENT_DIRECTORY}/libs
export INSTALL_DIR=${LIB_ROOT_DIR}/web
export SOURCE_DIR=${LIB_ROOT_DIR}/sources_web
export BUILD_DIR=${LIB_ROOT_DIR}/build

rm -fr ${BUILD_DIR}
mkdir -p ${LIB_ROOT_DIR}
mkdir -p ${INSTALL_DIR}
mkdir -p ${SOURCE_DIR}
mkdir -p ${BUILD_DIR}

export CC=emcc
export CXX=em++
export AR=emar
export AS=$CC
export LD=ld
export RANLIB=emranlib
export STRIP=emstrip

echo ${CURRENT_DIRECTORY}

if [ "#EMSDK" == "" ]; then
	echo "please install emsdk"
	exit 1
fi

# TOOLCHAIN 
if [ ! -x "$(command -v emcc)" ]; then
	source ${EMSDK}/emsdk_env.sh
fi

export CMAKE_TOOLCHAIN_FILE=${EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake

# =================================Install Lua===================================
LIB_VERSION=5.4.4
LIB_NAME=lua-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

# 安装目录不存在则进行安装
#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
	if [ ! -d ${LIB_SRC_DIR} ]; then
		if [ -f ${LIB_SRC_TAR_GZ} ]; then
			tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		else
			wget http://www.lua.org/ftp/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
			tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		fi
	fi

	cd ${LIB_SRC_DIR}

	sed -i '9s/ gcc -std=gnu99/'" ${CC}"'/' ${LIB_SRC_DIR}/src/Makefile
	sed -i '14s/ ar/'" ${AR}"'/' ${LIB_SRC_DIR}/src/Makefile
	sed -i '15s/ ranlib/'" ${RANLIB}"'/' ${LIB_SRC_DIR}/src/Makefile
	
	rm -fr ${LIB_SRC_DIR}/install

	make clean
	make VERBOSE=1
	make local 

	mv ${LIB_SRC_DIR}/install ${LIB_INSTALL_DIR}

	cd -
fi

# =================================Install LuaBridge===================================
LIB_VERSION=2.6
LIB_NAME=LuaBridge-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ghproxy.com/https://github.com/vinniefalco/LuaBridge/archive/refs/tags/${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    mkdir -p ${LIB_INSTALL_DIR}/include
    cp -fr ${LIB_SRC_DIR}/Source/LuaBridge ${LIB_INSTALL_DIR}/include/LuaBridge
fi


## =================================Install SDL2===================================
#LIB_VERSION=2.0.22
#LIB_NAME=SDL2-${LIB_VERSION}
#LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
#LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
#LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
#LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

##rm -fr ${LIB_INSTALL_DIR}
#if [ ! -d ${LIB_INSTALL_DIR} ]; then
    #if [ ! -d ${LIB_SRC_DIR} ]; then
        #if [ -f ${LIB_SRC_TAR_GZ} ]; then
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #else
            #wget https://ghproxy.com/https://github.com/libsdl-org/SDL/releases/download/release-${LIB_VERSION}/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #fi
    #fi

	#cd ${LIB_SRC_DIR}
	##make clean
	#./configure  --enable-static --enable-shared=no --enable-hidapi=no --with-pic --prefix=${LIB_INSTALL_DIR} --host=${TARGET} --with-sysroot=${SYSROOT}
	#make 
	#make install
    #cd -
#fi


# =================================Install libiconv===================================
LIB_VERSION=1.16
LIB_NAME=libiconv-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ftp.gnu.org/pub/gnu/libiconv/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
    emconfigure ./configure  --enable-static --enable-shared=no --enable-extra-encodings --without-libiconv-prefix --without-libintl-prefix --prefix=${LIB_INSTALL_DIR} 
    emmake make
    emmake make install
    cd -
fi


# =================================Install libuv===================================
LIB_VERSION=1.44.1
LIB_NAME=libuv-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
	if [ ! -d ${LIB_SRC_DIR} ]; then
		if [ -f ${LIB_SRC_TAR_GZ} ]; then
			tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		else
			wget https://ghproxy.com/https://github.com/libuv/libuv/archive/refs/tags/v${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
			tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		fi
	fi

	cd ${LIB_SRC_DIR}
	make clean
	bash autogen.sh
	emconfigure ./configure  --enable-static --enable-shared=no --prefix=${LIB_INSTALL_DIR} 
	emmake make
	emmake make install
	cd -
fi

# =================================Install openssl===================================
#LIB_VERSION=3.0.2
#LIB_NAME=openssl-${LIB_VERSION}
#LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
#LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
#LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
#LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
#OPENSSL_ROOT_DIR=${LIB_INSTALL_DIR}

#if [ ! -d ${LIB_INSTALL_DIR} ]; then
    #if [ ! -d ${LIB_SRC_DIR} ]; then
        #if [ -f ${LIB_SRC_TAR_GZ} ]; then
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #else
            #wget https://www.openssl.org/source/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #fi
    #fi

    #cd ${LIB_SRC_DIR}
	#make clean
    #./Configure --prefix=${LIB_INSTALL_DIR} 
    #make
    #make install
	#cp -fr ${LIB_INSTALL_DIR}/lib64 ${LIB_INSTALL_DIR}/lib
    #cd -
#fi

## =================================Install curl===================================
#LIB_VERSION=7.82.0
#LIB_NAME=curl-${LIB_VERSION}
#LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
#LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
#LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
#LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
#CURL_ROOT_DIR=${LIB_INSTALL_DIR}

##rm -fr ${LIB_INSTALL_DIR}
#if [ ! -d ${LIB_INSTALL_DIR} ]; then
    #if [ ! -d ${LIB_SRC_DIR} ]; then
        #if [ -f ${LIB_SRC_TAR_GZ} ]; then
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #else
            #wget https://curl.se/download/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #fi
    #fi

    #cd ${LIB_SRC_DIR}
	#make clean
    #./configure --enable-static --enable-shared=no --prefix=${LIB_INSTALL_DIR} --with-openssl=${OPENSSL_ROOT_DIR}
    #make
    #make install
    #cd -
#fi

# =================================Install tinyxml2===================================
LIB_VERSION=9.0.0
LIB_NAME=tinyxml2-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
		    wget https://ghproxy.com/https://github.com/leethomason/tinyxml2/archive/refs/tags/${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    mkdir -p ${LIB_BUILD_DIR}
    cd ${LIB_BUILD_DIR}
    cmake -S ${LIB_SRC_DIR} -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
	#make VERBOSE=1
	cmake --build . --verbose 
    make install 
    cd -
fi

# =================================Install spdlog===================================
LIB_VERSION=1.10.0
LIB_NAME=spdlog-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ghproxy.com/https://github.com/gabime/spdlog/archive/refs/tags/v${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    mkdir -p ${LIB_BUILD_DIR}
    cd ${LIB_BUILD_DIR}
    cmake -S ${LIB_SRC_DIR} -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
	#make VERBOSE=1
	cmake --build . --verbose 
    make install 
    cd -
fi

# =================================Install zlib===================================
LIB_VERSION=1.2.12
LIB_NAME=zlib-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
ZLIB_ROOT_DIR=${LIB_INSTALL_DIR}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget http://zlib.net/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
    emconfigure ./configure  --static --prefix=${LIB_INSTALL_DIR}
    emconfigure make 
    emconfigure make install
    cd -
fi

# =================================Install libpng===================================
LIB_VERSION=1.6.35
LIB_NAME=libpng-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
LIBPNG_ROOT_DIR=${LIB_INSTALL_DIR}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
	if [ ! -d ${LIB_SRC_DIR} ]; then
		if [ -f ${LIB_SRC_TAR_GZ} ]; then
			tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		else
			wget https://ghproxy.com/https://github.com/glennrp/libpng/archive/refs/tags/v${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
			tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		fi
	fi

	cd ${LIB_SRC_DIR}
	make clean
	emconfigure ./configure  --enable-static --enable-shared=no CPPFLAGS="-I${ZLIB_ROOT_DIR}/include" LDFLAGS="-L${ZLIB_ROOT_DIR}/lib" --prefix=${LIB_INSTALL_DIR}  
	emmake make 
	emmake make install
	cd -
fi


# =================================Install pixman===================================
LIB_VERSION=0.40.0
LIB_NAME=pixman-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
PIXMAN_ROOT_DIR=${LIB_INSTALL_DIR}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
	if [ ! -d ${LIB_SRC_DIR} ]; then
		if [ -f ${LIB_SRC_TAR_GZ} ]; then
			tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		else
			wget https://www.cairographics.org/releases/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
			tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		fi
	fi

	cd ${LIB_SRC_DIR}
	emconfigure ./configure --enable-static --enable-shared=no --prefix=${LIB_INSTALL_DIR} PNG_CFLAGS="-I${LIBPNG_ROOT_DIR}/include/libpng16" PNG_LIBS="-L${LIBPNG_ROOT_DIR}/lib -lpng16" 
	emmake make 
	emmake make install
	cd -
fi

# =================================Install freetype===================================
LIB_VERSION=2.12.0
LIB_NAME=freetype-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
FREETYPE_ROOT_DIR=${LIB_INSTALL_DIR}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://download.savannah.gnu.org/releases/freetype/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
	make clean
    emconfigure ./configure --enable-static --enable-shared=no \
		--with-bzip2=no \
		--prefix=${LIB_INSTALL_DIR} \
		LIBPNG_CFLAGS="-I${LIBPNG_ROOT_DIR}/include/libpng16" \
		LIBPNG_LIBS="-L${LIBPNG_ROOT_DIR}/lib -lpng16" \
		ZLIB_CFLAGS="-I${ZLIB_ROOT_DIR}/include" \
		ZLIB_LIBS="-L${ZLIB_ROOT_DIR}/lib -lz" 
    #BZIP2_CFLAGS="-I${BZIP2_ROOT_DIR}/include" BZIP2_LIBS="-L${BZIP2_ROOT_DIR}/lib -lbz2" 
	sed -i '113c CCraw_build  := gcc' builds/unix/unix-cc.mk
    emmake make 
    emmake make install
    cd -
fi

# =================================Install cairo===================================
LIB_VERSION=1.16.0
LIB_NAME=cairo-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

#rm -fr ${LIB_INSTALL_DIR}
#rm -fr ${LIB_SRC_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://www.cairographics.org/releases/${LIB_NAME}.tar.xz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi
	
    cd ${LIB_SRC_DIR}
	#make clean
	emconfigure ./configure ax_cv_c_float_words_bigendian=no --enable-static \
		--enable-script=no \
		--enable-shared=no \
		--enable-xlib-xcb=no \
		--enable-xcb=no \
		--enable-xcb-shm=no \
		--enable-xlib=no \
		--enable-xlib-xrender=no \
		--enable-fc=no \
		--enable-png=yes \
		--prefix=${LIB_INSTALL_DIR} \
		PKG_CONFIG_PATH="" \
		CFLAGS="-I${ZLIB_ROOT_DIR}/include" \
		CPPFLAGS="-I${ZLIB_ROOT_DIR}/include" \
		LDFLAGS="-L${ZLIB_ROOT_DIR}/lib" \
		LIBS="-lz" \
		png_REQUIRES="yes" \
		png_CFLAGS="-I${LIBPNG_ROOT_DIR}/include/libpng16" \
		png_LIBS="-L${LIBPNG_ROOT_DIR}/lib -lpng16" \
		FREETYPE_CFLAGS="-I${FREETYPE_ROOT_DIR}/include/freetype2" \
		FREETYPE_LIBS="-L${FREETYPE_ROOT_DIR}/lib -lfreetype" \
		pixman_CFLAGS="-I${PIXMAN_ROOT_DIR}/include/pixman-1" \
		pixman_LIBS="-L${PIXMAN_ROOT_DIR}/lib -lpixman-1" 

	cd src
    emmake make VERBOSE=1
    emmake make install
    cd -
fi
